package com.maint.m.modle

import android.Manifest
import android.bluetooth.BluetoothDevice
import android.content.ContentValues.TAG
import android.os.Build
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.maint.m.utils.Constants
import com.rokid.security.phone.sdk.api.PSecuritySDK
import com.rokid.security.phone.sdk.api.bluetooth.classic.listener.IClassicBTClientListener
import com.rokid.security.phone.sdk.api.wifip2p.listener.IWifiP2PClientListener
import com.rokid.security.phone.sdk.base.data.EngineParam
import com.rokid.security.phone.sdk.base.data.EnvType
import com.rokid.security.phone.sdk.base.data.RtcConfig
import com.rokid.security.phone.sdk.base.data.ServiceBusConfig
import com.rokid.security.phone.sdk.base.data.UserAuthInfo
import com.rokid.security.phone.sdk.base.data.UserInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.nio.ByteBuffer
import java.time.Instant
import java.time.temporal.TemporalAmount
import java.util.Date
import kotlin.time.Duration

open class BaseViewModel : ViewModel() {

    var isWifiConnect = false
    var isBtConnect = false
    var mGson = Gson()
    companion object {
        lateinit var instance: BaseViewModel
    }

    private val mAbsSecurityEngineService by lazy {
        PSecuritySDK.getMobileEngineService()
    }

    private val TAG = "BaseViewModel::"
    private var deviceList = mutableListOf<BluetoothDevice>()
    private var isConnected = false;
    private lateinit var device: BluetoothDevice
    private val mAbsBlueToothClientService by lazy {
        PSecuritySDK.getClassicBlueToothClientService()
    }

    private var mUserInfo: UserInfo? = null
    fun initSDK() {
        instance = this

        viewModelScope.launch(Dispatchers.IO) {
            val userAuthInfo = UserAuthInfo("","")
            Log.i(TAG + "userAuthInfo:", userAuthInfo.toString())

            val userInfo = UserInfo(
                "", "", "",
                ""
            )
            Log.i(TAG + "userInfo:", userInfo.toString())

            val rtcConfig = RtcConfig("", "", "", "", "")
            Log.i(TAG + "rtcConfig:", rtcConfig.toString())

            var serviceBusConfig = ServiceBusConfig("", "userInfo", arrayListOf())
            Log.i(TAG + "serviceBusConfig:", serviceBusConfig.toString())

            val param = EngineParam(
                Constants.ServerId, serviceBusConfig, userInfo, rtcConfig,
                "", "",userAuthInfo
            )
            Log.i(TAG + "param:", param.toString())

            mUserInfo = userInfo
            mAbsSecurityEngineService.initSDK(param) {
                Log.i(TAG, "initSDK: $it")
                if (it.isSuccess) {
                }
            }

        }

    }

    fun destroySDK() {
        mAbsSecurityEngineService.destroy()
        PSecuritySDK.getClassicBlueToothClientService()
            ?.removeClientListener(mIClassicBTClientListener)
        PSecuritySDK.getWifiP2PClientService()?.removeWifiP2PClientListener(mIWifiP2PClientListener)

    }


}


private val mIWifiP2PClientListener = object : IWifiP2PClientListener {
    override fun onWifiP2pEnabled(enabled: Boolean) {
        Log.i(TAG, "onWifiP2pEnabled:  $enabled")


    }
}
private val mIClassicBTClientListener = object : IClassicBTClientListener {
    override fun onDeviceFound(device: BluetoothDevice) {

    }

    override fun onScanFinished() {

    }

    override fun onConnect(success: Boolean) {
//            MyApplication.instance.isBtConnect = success
//            lifecycleScope.launch {
//                if(!success){
//                    connectStatus(false)
//                }else{
//                    binding.tvDeviceName.text = DeviceLinkerManager.getDeviceName()
//                }
//            }
    }
    
    override fun onConnectionRejected(reason: String, code: Int) {
        // 处理连接被拒绝的情况
    }


}